Appendix B — Assignment A

Instructions

  1. You may talk to a friend, discuss the questions and potential directions for solving them. However, you need to write your own solutions and code separately, and not as a group activity.

  2. Do not write your name on the assignment.

  3. Write your code in the Code cells of the Jupyter notebook. Ensure that the solution is written neatly enough to understand and grade.

  4. Use Quarto to print the .ipynb file as HTML. You will need to open the command prompt, navigate to the directory containing the file, and use the command: quarto render filename.ipynb --to html. Submit the HTML file.

  5. There are 5 points for clealiness and organization. The breakdow is as follows:

  • Must be an HTML file rendered using Quarto (1.5 pts).

  • There aren’t excessively long outputs of extraneous information (e.g. no printouts of unnecessary results without good reason, there aren’t long printouts of which iteration a loop is on, there aren’t long sections of commented-out code, etc.) (1 pt)

  • There is no piece of unnecessary / redundant code, and no unnecessary / redundant text (1 pt)

  • The code should be commented and clearly written with intuitive variable names. For example, use variable names such as number_input, factor, hours, instead of a,b,xyz, etc. (1.5 pts)

  1. The assignment is worth 100 points, and is due on 13th April 2023 at 11:59 pm.

B.1 Alarm clock

B.1.1 When does the alarm go off?

You look at the clock and it is exactly 2pm. You set an alarm to go off in 510 hours. At what time does the alarm go off? If the answer is say, 4 pm, then your code should print - "The alarm goes off at 4 pm".

(2 points)

B.1.2 User-friendly alarm clock

Write a program to solve the general version of the above problem. Ask the user for - (1) the time now (in hours), and (2) the number of hours for the alarm to go off. Your program should output the time at which the alarm goes off. Both the user inputs must be in {0, 1, 2…, 22, 23}. If the answer is, say 14:00 hours, then your program should print - “The alarm goes off at 14:00 hours.

Show the output of your program when the user inputs 7 as the current time, and 95 as the number of hours for the alarm to go off.

(4 points)

B.2 Finding prime factors

B.2.1 Prime or not

Write a program that checks if a positive integer is prime or not. Show the output when the program is used to check if 89 is prime or not.

(2 points)

B.2.2 Factors

Prompt the user to input a positive integer. Write a program that prints the factors of the positive integer input by the user. Show the output of the program if the user inputs 190.

(2 points)

B.2.3 Prime factors

Prompt the user to input a positive integer. Update the program in 2(b) to print the prime factors of the positive integer input by the user. Show the output of the program if the user inputs 190.

(8 points)

B.2.4 User-friendly prime factor calculator

Update the program in 2(c), so that it prints “Incorrect input, please enter positive integer” if the user does not enter a positive integer, and then prompts the user to input a positive integer. The program should continue to prompt the user to enter a positive integer until the user successfully enters a positive integer. Show the output of the program if the user enters "seventy" in the first attempt, "#70" in the second attempt, and 70 in the third attempt.

(12 points)

B.3 Number of words in a sentence

Prompt the user to input an english sentence. Write a program that counts and prints the number of words in the sentence input by the user. The program should continue to run until the user inputs the sentence - “end program”. Show the output of the program if the user enters "this is the time to sleep" in the first attempt, "this is too much work for a day" in the second attempt, and "end program" in the third attempt.

Hint: Count the number of spaces

(10 points)

B.4 Survival of rabbits

In many environments, two or more species compete for the available resources. Classic predator–prey equations have been used to simulate or predict the dynamics of biological systems in which two species interact, one as a predator and the other as prey. You will use a simplified version of the Lotka-Volterra equations for modeling fox/rabbit populations, described below.

Let the following variables be defined as:

\(r_t\): The number of prey (rabbits) at time \(t\), where \(t\) corresponds to a certain year.

\(f_t\): The number of predators (foxes) at time \(t\), where \(t\) corresponds to a certain year.

\(\alpha\): The birth rate of prey.

\(\beta\): The death rate of prey (depends on predator population).

\(\gamma\): The birth rate of predators (depends on prey population).

\(\delta\): The death rate of predators.

Then, we can define the populations of the next time period or the next year \((t+1)\) using the following system of equations:

\[r_{t+1} = r_t + \alpha r_t - \beta r_t f_t,\]

\[f_{t+1} = f_t + \gamma f_t r_t - \delta f_t\]

B.4.1 Number of rabbits and foxes

Write a program that uses the following parameter values, and calculates and prints the populations of the rabbits and foxes for each year upto the next 14 years. Since the number of rabbits and foxes cannot be floating-point numbers, use the in-built python function round() to round-off the calculated values to integers. Also, we cannot have negative rabbits or negative foxes, so if the population values are ever negative, consider the population to be zero instead.

\(r_0\) = 500

\(f_0\) = 1

\(\alpha\) = 0.2

\(\beta\) = 0.005

\(\gamma\) = 0.001

\(\delta\) = 0.2

The output of the program should be as follows:

At time t = 0, there are 500 rabbits, and 1 foxes

At time t = 1, there are 598 rabbits, and 1 foxes

At time t = 2, there are 713 rabbits, and 2 foxes

At time t = 3, there are 849 rabbits, and 3 foxes

At time t = 4, there are 1007 rabbits, and 5 foxes

At time t = 5, there are 1186 rabbits, and 8 foxes

At time t = 6, there are 1375 rabbits, and 16 foxes

At time t = 7, there are 1538 rabbits, and 35 foxes

At time t = 8, there are 1573 rabbits, and 83 foxes

At time t = 9, there are 1237 rabbits, and 196 foxes

At time t = 10, there are 270 rabbits, and 400 foxes

At time t = 11, there are 0 rabbits, and 428 foxes

At time t = 12, there are 0 rabbits, and 342 foxes

At time t = 13, there are 0 rabbits, and 274 foxes

At time t = 14, there are 0 rabbits, and 219 foxes

(10 points)

B.4.2 How long can 100 rabbits survive?

Suppose at \(t = 0\), there are 100 rabbits, i.e., \(r_0 = 100\). How many foxes should be there at \(t = 0\) (i.e., what should be \(f_0\)), such that the rabbit species survives (i.e., \(r_{t\_max}>0\)) for the maximum possible number of years (\(t_{max}\)) before becoming extinct (i.e., \(r_{t\_max+1} = 0\)). Also, find the maximum possible number of years (i.e., \(t_{max}\)) the rabbit species will survive.

Modify the program in the previous question to compute the answers to the above questions, and print the following statement, with the blanks filled:

If there are ___ foxes at t = 0, the rabbit species will survive for ___ years, which is the maximum possible number of years they can survive.

Note: Use the same values of \(\alpha\), \(\beta\), \(\gamma\), and \(\delta\) as in the previous question.

Hint:

  1. Consider values of \(f_0\) starting from 1, and upto a large number, say 1000.

  2. For each value of \(f_0\), find the number of years for which the rabbit species survives.

  3. Find the value of \(f_0\) and \(t\) for which the rabbit species surives the maximum number of years, i.e., \(t = t_{max}\).

(20 points)

B.4.3 Saving rabbits from extinction

What must be the minimum number of rabbits, and the corresponding number of foxes at \(t = 0\), such that the rabbit and fox species never become extinct.

Note: Use the same values of \(\alpha\), \(\beta\), \(\gamma\), and \(\delta\) as in the previous question.

Hint:

  1. Consider \(r_0 = 1\), and then keep increasing \(r_0\) by 1 if it’s not possible for the rabbit species to survive with the value of \(r_0\) under consideration.

  2. For each \(r_0\), consider number of foxes starting from \(f_0 = 1\), and upto a large number, say \(f_0 = 200\).

  3. As soon as you find a combination of \(r_0\) and \(f_0\), such that there is no change in \(r_t\) and \(f_t\) for 2 consecutive years, you have found the values of \(r_0\) and \(f_0\), such that both the species maintain their numbers and never become extinct. At this point, print the result, and stop the program (break out of all loops).

Modify the program in the previous question to answer the above question, and print the following statement with the blanks filled:

For ___ foxes, and ___ rabbits at t = 0, the fox and rabbit species will never be extinct.

(25 points)